//
//  DestCollectionFlowLayout.m
//  TravelEveryDay
//
//  Created by dlios on 15/10/26.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "DestCollectionFlowLayout.h"
#import "BaseHeader.h"



@implementation DestCollectionFlowLayout

- (void)prepareLayout {
    [super prepareLayout];
    _maxwidth = 0.0f;
    //用来存放item的属性设置
    _array = [[NSMutableArray alloc]init];
    CGFloat first = 0.f * WIDTH;
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < count; i ++) {
        NSIndexPath *indexPath = [NSIndexPath  indexPathForItem:i inSection:0];//获取下标
          UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        //通过下标找到item
        CGSize itemSize = [self.delegate collectionView:self.collectionView layout:self sizeForItemAtIndexPath:indexPath];
         CGFloat offsetX = 0 * HEIGHT;//坐标
        CGFloat maxLeft = 0.0f;//总高度
        attributes.frame = CGRectMake(first, offsetX, itemSize.width, itemSize.height);
        //判断 最后一个  不给 加5  让他 最后的item和collectionView一样的
        if (i == count -1) {
        maxLeft = first +itemSize.width ;
        }else{
        maxLeft = first +itemSize.width +5 *WIDTH;//左边宽度综合
        }
        first += itemSize.width + 5 * WIDTH;//下行item的初始值

        self.maxwidth = maxLeft;
        [self.array addObject:attributes];
    }
}
//重写系统 方法 初始的layout外形将由该方法返回的uicollectionViewAttributes来决定
//返回rect中的  所有元素的布局属性
//返回的是包含uicollectionViewlayoutAttributes的 数组
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (int i = 0; i < self.array.count; i ++) {
        UICollectionViewLayoutAttributes *attributess = self.array [i];
  
            [arr addObject:attributess];

        
    }
    
    return arr;
}

//重新设置滚动范围
- (CGSize)collectionViewContentSize {
    return CGSizeMake( self.maxwidth ,self.collectionView.frame.size.height );
}

@end
